package com.fs.ai_qa_platform.scoring;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fs.ai_qa_platform.exception.BusinessException;
import com.fs.ai_qa_platform.manager.AiManager;
import com.fs.ai_qa_platform.model.dto.question.QuestionAnswerDTO;
import com.fs.ai_qa_platform.model.dto.question.QuestionContentDTO;
import com.fs.ai_qa_platform.model.entity.App;
import com.fs.ai_qa_platform.model.entity.Question;
import com.fs.ai_qa_platform.model.entity.UserAnswer;
import com.fs.ai_qa_platform.model.vo.QuestionVO;
import com.fs.ai_qa_platform.service.QuestionService;
import com.fs.ai_qa_platform.service.ScoringResultService;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;

import javax.annotation.Resource;
import javax.jws.soap.SOAPBinding;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * Ai测评类应用评分策略
 */
@ScoringStrategyConfig(appType = 1,scoringStrategy = 1)

public class AiTestScoringStrategy implements ScoringStrategy {

    @Resource
    private QuestionService questionService;

    @Resource
    private ScoringResultService scoringResultService;

    @Resource
    private AiManager aiManager;

    @Resource
    private RedissonClient redissonClient;

//  分布式锁的key
    private final String AI_ANSWER_LOCK = "AI_ANSWER_LOCK";

    /**
     * AI评分结果缓存
     */
    private final Cache<String,String> answerCacheMap =
            Caffeine.newBuilder().newBuilder().initialCapacity(1024)
                    .expireAfterAccess(5L, TimeUnit.MINUTES)
                    .build();

    private static final String AI_TEST_SCORING_SYSTEM_MESSAGE = "你是一位严谨的判题专家，我会给你如下信息：\n" +
            "```\n" +
            "应用名称，\n" +
            "【【【应用描述】】】，\n" +
            "题目和用户回答的列表：格式为 [{\"title\": \"题目\",\"answer\": \"用户回答\"}]\n" +
            "```\n" +
            "\n" +
            "请你根据上述信息，按照以下步骤来对用户进行评价：\n" +
            "1. 要求：需要给出一个明确的评价结果，包括评价名称（尽量简短）和评价描述（尽量详细，大于 200 字）\n" +
            "2. 严格按照下面的 json 格式输出评价名称和评价描述\n" +
            "```\n" +
            "{\"resultName\": \"评价名称\", \"resultDesc\": \"评价描述\"}\n" +
            "```\n" +
            "3. 返回格式必须为 JSON 对象";



    @Override
    public UserAnswer doScore(List<String> choices, App app) throws Exception {
        Long appId = app.getId();
        String jsonStr = JSONUtil.toJsonStr(choices);
        String cacheKey = buildCacheKey(appId, jsonStr);
        String answerJson = answerCacheMap.getIfPresent(cacheKey);
        //判断缓存是否存在结果
        if(StrUtil.isNotBlank(answerJson)){
            UserAnswer userAnswer = JSONUtil.toBean(answerJson,UserAnswer.class);
            userAnswer.setAppId(appId);
            userAnswer.setAppType(app.getAppType());
            userAnswer.setScoringStrategy(app.getScoringStrategy());
            userAnswer.setChoices(jsonStr);
            return userAnswer;
        }
        //定义锁
        RLock lock =  redissonClient.getLock(AI_ANSWER_LOCK + cacheKey);

        try{
            //竞争锁
            boolean res = lock.tryLock(3,15,TimeUnit.SECONDS);
            if(!res){
                //强行返回
                return null;
            }

            // 1. 根据 id 查询到题目信息
            Question question = questionService.getOne(
                    Wrappers.lambdaQuery(Question.class).eq(Question::getAppId, appId)
            );

            //获取题目列表
            QuestionVO questionVO = QuestionVO.objToVo(question);
            List<QuestionContentDTO> questionContent = questionVO.getQuestionContent();

            //2. 调用ai获取结果
            //封装prompt
            String userMessage = getAiTestScoringUserMessage(app, questionContent, choices);
            //Ai生成
            String result = aiManager.doSyncStableRequest(AI_TEST_SCORING_SYSTEM_MESSAGE,userMessage);
            // 截取需要的 JSON 信息
            int start = result.indexOf("{");
            int end = result.lastIndexOf("}");
            String json = result.substring(start, end + 1);

            //缓存结果
            answerCacheMap.put(cacheKey,json);

            // 3. 构造返回值，填充答案对象的属性
            UserAnswer userAnswer = JSONUtil.toBean(json,UserAnswer.class);
            userAnswer.setAppId(appId);
            userAnswer.setAppType(app.getAppType());
            userAnswer.setScoringStrategy(app.getScoringStrategy());
            userAnswer.setChoices(JSONUtil.toJsonStr(choices));
            return userAnswer;
        }finally {
            if(lock !=null&&lock.isLocked()){
                if(lock.isHeldByCurrentThread()){
                    lock.unlock();
                }
            }
        }
    }

    /**
     * 封装用户Prompt
     * @param app
     * @param questionContentDTOList
     * @param choices
     * @return
     */
    private String getAiTestScoringUserMessage(App app, List<QuestionContentDTO> questionContentDTOList, List<String> choices) {

        StringBuilder userMessage = new StringBuilder();
        userMessage.append(app.getAppName()).append("\n");
        userMessage.append(app.getAppDesc());

//        Long appId = app.getId();
//        List<ScoringResult> scoringResultList = scoringResultService.list(
//                Wrappers.lambdaQuery(ScoringResult.class)
//                        .eq(ScoringResult::getAppId, appId)
//        );
//        if (scoringResultList!=null){
//            List<String> scoringResultNameList = scoringResultList.stream()
//                            .map(ScoringResult::getResultName).collect(Collectors.toList());
//            userMessage.append("结果必须是"+ scoringResultNameList +"中的一个");
//        }
        userMessage.append("\n");
        List<QuestionAnswerDTO> questionAnswerDTOList = new ArrayList<>();
        for (int i = 0; i < questionContentDTOList.size(); i++) {
            QuestionAnswerDTO questionAnswerDTO = new QuestionAnswerDTO();
            questionAnswerDTO.setTitle(questionContentDTOList.get(i).getTitle());
            questionAnswerDTO.setUserAnswer(choices.get(i));
            questionAnswerDTOList.add(questionAnswerDTO);
        }
        userMessage.append(JSONUtil.toJsonStr(questionAnswerDTOList));
        return userMessage.toString();
    }

    /**
     *
     * @param appId
     * @param choices
     * @return
     */
    private String buildCacheKey(Long appId,String choices){
        return DigestUtil.md5Hex(appId + ":" + choices);
    }
}